<?php
require_once('Kbs/Model/Business/Abstract.php');

class Kbs_Model_Business_Post_Category extends Kbs_Model_Business_Abstract
{
    protected $_mapperClassName = 'Kbs_Model_Mapper_Post_Category';

    protected $_categoryId;
    protected $_categoryUrlTitle;

    /**
     * Post
     *
     * @var Kbs_Model_Business_Post
     */
    protected $_post = null;

    /**
     * Category details
     *
     * @var Kbs_Model_Business_Post_Category_Detail
     */
    protected $_details = null;

    /**
     * Category detail by current locale
     *
     * @var Kbs_Model_Business_Post_Category_Detail
     */
    protected $_detailByLocale = null;

    /**
     * Get id
     */
    public function getId()
    {
        return $this->getCategoryId();
    }

    public function setId($id)
    {
        return $this->setCategoryId($id);
    }

    /**
     * Get category id
     */
    public function getCategoryId()
    {
        return (int)$this->_categoryId;
    }

    public function setCategoryId($id)
    {
        $this->_categoryId = (int)$id;
        return $this;
    }

    /**
     * Get locale title
     */
    public function getCategoryUrlTitle()
    {
        return (string)$this->_categoryUrlTitle;
    }

    public function setCategoryUrlTitle($string)
    {
        $this->_categoryUrlTitle = (string)$string;
        return $this;
    }

    /**
     * Fetch details for the post
     *
     *
     */
    public function fetchDetails()
    {
        if (null == $this->_details) {
            $detailObj = new Kbs_Model_Business_Post_Category_Detail();
            $this->_details = $detailObj->findByDetailCategoryId($this->categoryId);
        }
        return $this->_details;
    }

    /**
     * Fetch detail object for the category
     *
     * @return Kbs_Model_Business_Post_Category_Detail|false
     */
    public function fetchDetailByLocale($localeId = null)
    {
        //if (null == $this->_detailByLocale) {
        if (null == $localeId) {
            $session = new Zend_Session_Namespace('locale');
            $localeId = $session->localeId;
        }
        $detail = new Kbs_Model_Business_Post_Category_Detail();
        $detail = $detail->findByParams(array('detailCategoryId' => $this->categoryId, 
                                              'detailLocaleId' => $localeId));
        $this->_detailByLocale = $detail[0];
        //}
        return $this->_detailByLocale;
    }

    /**
     * To fetch the categories list
     *
     * @return array|false $result
     */
    public function getCategoriesListByLocale($categoryId = null)
    {
        if (!empty($categoryId)) {
            $categories = $this->find($categoryId);
        } else {
            $categories = $this->fetchAll();
        }
        $result = array();
        if (!empty($categories)) {
            foreach ($categories as $category) {
                // If we can't get the details then we ignore it !
                if ($category->fetchDetailByLocale()) {
                    $title = trim($category->fetchDetailByLocale()->detailTitle);
                    if (!empty($title)) {
                        $result[] = $category;
                    }
                }
            }
        }
        //var_dump($result);
        //die;
        return $result;
    }

    /**
     *
     */
    public function getCategoryOptions()
    {
        $result = array(0 => 'Unselected');
        $categories = $this->fetchAll();
        if ($categories) {
            foreach ($categories as $category) {
                $result[$category->categoryId] = $category->categoryUrlTitle;
            }
        }
        return $result;
    }

    public function saveCategoryAndDetails($data)
    {
        //var_dump($data);die;
        $this->setCategoryId($data['categoryId'])
             ->setCategoryUrlTitle($data['categoryUrlTitle']);
        $newId = $this->save();
        $localeObj = new Kbs_Model_Business_Locale();
        foreach ($localeObj->fetchAll() as $locale) {

            $categoryDetailObj = new Kbs_Model_Business_Post_Category_Detail();
            $categoryDetailObj->setDetailId($data['detailId'][$locale->localeId])
                              ->setDetailCategoryId($newId)
                              ->setDetailLocaleId($locale->localeId)
                              ->setDetailTitle($data['detailTitle'][$locale->localeId]);
            $categoryDetailObj->save();
        }
    }

    /**
     * Delete the post
     *
     */
    public function deleteCategoryAndDetails($id)
    {
        $this->find($id);
        $this->delete();

        $detail = new Kbs_Model_Business_Post_Category_Detail();
        $details = $detail->findByDetailCategoryId($id);
        if (!empty($details)) {
            foreach ($details as $detail) {
                $detail->delete();
            }
        }
    }


}